#include <Windows.h>
#include <stdio.h>
#include <conio.h>
#include <time.h>
#include <stdlib.h>
#include <dos.h>
//init variable
int Bdo[100][100];
struct ran{  
	int x, y;//toa do mot phan than con ran
	int tt; //0 :khong co than ran, 1 len, 2 phai, 3 xuong, 4 trai
};

ran Conran[10000]; 
int Randai = 3; // do dai cua ran
bool an = false;//kiem tra thuc an cua ran
//khoi tao toa do thuc an ban dau
int xta = 0,yta = 0;

//prototype
void xuli();
void initBando();//khoi tao ban do;
void initRan();//khoi tao do dai ran ban dau bang 3
void tdthucan();
void Ranxh(int x, int y);
void gotoxy(int, int);

int main(){
	xuli();
	return 0;
}

void xuli(){
	int c,x,y,xdu = 0,ydu = 0;
	int tt1;//trang thai dau ran luc dau
	for (int i = 2; i <= 23; i++){
		for (int j = 2; j <= 59; j++) Bdo[i][j] = 0;
	}
	bool ngung = false;
	initRan();
	while (!ngung){
		Ranxh(xdu, ydu);
		//toa do dau con ran hien tai
		x = Conran[1].x;//toa do x dau ran, truoc khi no di chuyen
		y = Conran[1].y;//toa di y dau ran, truoc khi no di chuyen
		tt1 = Conran[1].tt; //Lay trang thai dau ran truoc khi no di chuyen
		Sleep(100);
		if (_kbhit()){ 
			c = _getch();
			c = _getch();
			switch (c){ // kiem tra phim duoc nhan
			case 72://phim mui ten di len
				if (Conran[1].tt == 3){ //neu ran dang di xuong, thi tiep tuc di xuong
					Conran[1].y += 1;
				}
				else{
					Conran[1].y -= 1;
					Conran[1].tt = 1;
				}
				break;
			case 77:// phim mui ten sang phai
				if (Conran[1].tt == 4){ //neu ran dang di sang trai, thi tiep tuc di
					Conran[1].x -= 1;
				}
				else{
					Conran[1].x += 1;
					Conran[1].tt = 2;
				}
				break;
			case 80:// phim mui ten xuong duoi
				if (Conran[1].tt == 1){
					Conran[1].y -= 1;
				}
				else{
					Conran[1].y += 1;
					Conran[1].tt = 3;
				}
				break;
			case 75://phim mui ten sang trai
				if (Conran[1].tt == 2){
					Conran[1].x += 1;
				}
				else{
					Conran[1].x -= 1;
					Conran[1].tt = 4;
				}
				break;
			}//ket thuc switch(c)
		} else{
			switch (Conran[1].tt){
			case 1:
				Conran[1].y -= 1;
				break;
			case 2:
				Conran[1].x += 1;
				break;
			case 3:
				Conran[1].y += 1;
				break;
			case 4:
				Conran[1].x -= 1;
				break;
			}//Ket thuc switch(Conran[1].tt)
		}
		//Kiem tra o ran vua di toi co dieu gi moi me
		if (Bdo[Conran[1].y][Conran[1].x] == 1 || Bdo[Conran[1].y][Conran[1].x] == 2){// neu dau ran dung trung thanh, hoac than cua no thi ket thuc
			ngung = true;
		}
		else if (Bdo[Conran[1].y][Conran[1].x] == 3){// tim duoc thuc an
			Randai += 1;
			xta = 0; yta = 0;
			Bdo[Conran[1].y][Conran[1].x] = 1;
			for (int i = Randai; i >= 3; i--){
				Conran[i].tt = Conran[i - 1].tt;
				Conran[i].x = Conran[i - 1].x;
				Conran[i].y = Conran[i - 1].y;
			}
			Conran[2].tt = tt1;
			Conran[2].x = x;
			Conran[2].y = y;
		}
		else{ //if o ban do la so 0, tuc khong co gi 
			xdu = Conran[Randai].x;
			ydu = Conran[Randai].y;
			Bdo[Conran[Randai].y][Conran[Randai].x] = 0;
			Bdo[Conran[1].y][Conran[1].x] = 1;
			for (int i = Randai; i >= 3; i--){
				Conran[i].tt = Conran[i - 1].tt;
				Conran[i].x = Conran[i - 1].x;
				Conran[i].y = Conran[i - 1].y;
			}
			Conran[2].tt = tt1;
			Conran[2].x = x;
			Conran[2].y = y;
		}
	}
	gotoxy(11, 20);
	printf("Ban da thua!");
}



void initBando(){
	gotoxy(1, 1);
	printf("%c",(char)201);
	//tao bien gioi tren
	for (int i = 2; i <= 60; i++){
		gotoxy(i,1);
		Bdo[1][i] = 2;
		printf("%c",(char)205);
	}
	//thanh doc ben trai
	for (int i = 2; i < 24; i++){
		gotoxy(1, i);
		Bdo[i][1] = 2;
		printf("%c",(char)186);
	}
	//thanh doc ben phai
	gotoxy(61, 1);
	printf("%c",(char)187);
	for (int i = 2; i < 24; i++){
		gotoxy(61, i);
		Bdo[i][61] = 2;
		printf("%c",(char)186);
	}
	gotoxy(1, 24);
	printf("%c",(char)200);
	gotoxy(61, 24);
	//xuat thanh ben duoi
	printf("%c",(char)188);
	for (int i = 2; i <= 60; i++){
		gotoxy(i, 24);
		Bdo[24][i] = 2;
		printf("%c",(char)205);
	}
}

void initRan(){
	for (int i = 1; i <= 100; i++) Conran[i].tt = 0;//tao con ran ban dau chua co gi
	int xd=28, yd=10; //toa do dau ran
	gotoxy(xd, xd);
	for (int i = 1; i <= 3; i++){
		Conran[i].tt = 1;
		Conran[i].x = xd;
		Conran[i].y = yd + i;
	}
}



void tdthucan(){
	int vtx,vty;
	do{
	srand((unsigned)time(NULL));
	vtx = rand() % (58-3) + 3;
	vty = rand() % (20-3) + 3;
	} while ((Bdo[vty][vtx] == 2) || (Bdo[vty][vtx] == 1)); //Neu la thanh hoac than ran thi tiep tuc
	Bdo[vty][vtx] = 3;
	gotoxy(xta, yta);//xoa vi tri thuc an truoc
	printf(" ");
	xta = vtx; yta = vty;
}

void Ranxh(int x, int y){//xoa duoi toa do x,y
	gotoxy(x, y);
	printf(" ");
	initBando();
	if (xta == 0 && yta == 0) tdthucan(); // neu thuc an da duoc an thi tao moi
	gotoxy(xta, yta);
	printf("%c", (char)254);
	for (int i = 1; i <= Randai; i++){
		gotoxy(Conran[i].x, Conran[i].y);
		switch (Conran[i].tt){
		case 1:
			printf("%c",(char)30);
			break;
		case 2:
			printf("%c",(char)16);
			break;
		case 3:
			printf("%c",(char)31);
			break;
		case 4:
			printf("%c",(char)17);
			break;
		}
	}
}
void gotoxy(int x, int y){
	COORD c;
	c.X = x;
	c.Y = y;
	SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), c);
}